package com.aaa.shuke2024.service.impl;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2024/12/3 11:31
 * @description：我的邮件发送类
 * @modified By：
 * @version: 1.0
 */
@Service
@Log
public class MyEmailService {
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;
    /**
     * @create by: Teacher陈
     * @description: 发送邮件
     * @create time: 2024/12/3 11:34
     * @param to 目标邮箱
     * @param subject 主题
     * @param content 内容
     * @return void
     */
  @Async
    public void sendMail(String to,String subject,String  content)  {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
      MimeMessageHelper helper= null;
      try {
          helper = new MimeMessageHelper(mimeMessage,true);
          //设置发件人
          helper.setFrom(from);
          //设置收件人
          helper.setTo(to);
          //设置主题
          helper.setSubject(subject);
          //设置内容
          helper.setText(content);
          mailSender.send(mimeMessage);
          log.info("发送告警邮件成功！"+content);
      } catch (MessagingException e) {
          throw new RuntimeException(e);
      }

    }
}
